package service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import bean.Course;
import bean.CourseBehave;
import bean.Course_Student;
import bean.SingleCourse;
import bean.Student;
import service.FileService;

@Service @Transactional
public class FileServiceBean implements FileService{
	@Resource SessionFactory factory;

	@Override
	public int getSidBySno(String sno) {
		return (int)factory.getCurrentSession().createQuery("select sid from Student where sno = ?")
				.setParameter(0, sno).list().get(0);
	}

	@Override
	public int getCidByCno(String cno) {
		return (int)factory.getCurrentSession().createQuery("select cid from Course where cno = ?")
				.setParameter(0, cno).list().get(0);
	}

	@Override
	public int addStudentsAndCourses(List<Student> studentlist, List<Course> courselist) {
		for(Student student : studentlist)
		{
			if(!factory.getCurrentSession().createQuery("select sid from Student where sno = ?")
					.setParameter(0, student.getSno()).list().isEmpty())				//是否存在原学号
			{
				student.setSid((int)factory.getCurrentSession().createQuery("select sid from Student where sno = ?")
						.setParameter(0, student.getSno()).list().get(0));			//由于sno的唯一约束，此处无法使用merge进行更新或添加，下同	
			}
			factory.getCurrentSession().merge(student);		//添加数据
		}
		for(Course course : courselist)
		{
			if(!factory.getCurrentSession().createQuery("select cid from Course where cno = ?")
					.setParameter(0, course.getCno()).list().isEmpty())				
			{
				course.setCid((int)factory.getCurrentSession().createQuery("select cid from Course where cno = ?")
						.setParameter(0, course.getCno()).list().get(0));				
			}
			factory.getCurrentSession().merge(course);		
		}
		return 1;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int addStu_couAndSingleCourseAndCourseBehave(
			int tid, List<Course_Student> course_Studentlist, List<SingleCourse> singleCourselist) 
	{
		List<Student> studentlist = new ArrayList<>();
		List<Integer> sidlist = new ArrayList<>();
		Student student;
		for(Course_Student course_Student : course_Studentlist)
		{
			if(factory.getCurrentSession().createQuery("select id from Course_Student where cid = ? and sid = ? and teacher_id = ?")
					.setParameter(0, course_Student.getCid())
					.setParameter(1, course_Student.getSid())
					.setParameter(2, tid)
					.list().isEmpty())		
			{
				factory.getCurrentSession().merge(course_Student);
			}
			
		}
		for(SingleCourse singleCourse : singleCourselist)
		{
			if(!factory.getCurrentSession().createQuery("select scid from SingleCourse where cid = ? and teacher_id = ? and week = ?")
			.setParameter(0, singleCourse.getCid())
			.setParameter(1, tid)
			.setParameter(2, singleCourse.getWeek())
			.list().isEmpty())
			{
				singleCourse.setScid((int)factory.getCurrentSession().createQuery("select scid from SingleCourse where cid = ? and teacher_id = ? and week = ?")
						.setParameter(0, singleCourse.getCid())
						.setParameter(1, tid)
						.setParameter(2, singleCourse.getWeek())
						.list().get(0));
				singleCourse.setHaspoint((int)factory.getCurrentSession().createQuery("select haspoint from SingleCourse where cid = ? and teacher_id = ? and week = ?")
						.setParameter(0, singleCourse.getCid())
						.setParameter(1, tid)
						.setParameter(2, singleCourse.getWeek())
						.list().get(0));
			}
			factory.getCurrentSession().saveOrUpdate(singleCourse);		//如果此处用merge将无法获取singleCourse保存后生成的主键scid
		//	System.out.println(1111 + singleCourse.getScid());
			sidlist = factory.getCurrentSession()
					.createQuery("select sid from Course_Student where cid = ?")		
					.setParameter(0, singleCourse.getCid()).list();						//获取该课堂对应的学生sid
			for(Integer sid : sidlist)									//循环生成单节课程表
	        {
				student = (Student) factory.getCurrentSession().get(Student.class, sid);		//获取该课堂对应的学生完整信息
				if(factory.getCurrentSession().createQuery("select cbid from CourseBehave where scid = ? and teacher_id = ? and sno = ?")
					.setParameter(0, singleCourse.getScid())
					.setParameter(1, tid)
					.setParameter(2, student.getSno())
					.list().isEmpty())
				{
		            CourseBehave courseBehave = new CourseBehave();
		            courseBehave.setSname(student.getSname());
		            courseBehave.setSno(student.getSno());
		            courseBehave.setPointstate("未点名");
		            courseBehave.setColorid(0);
		            courseBehave.setNeedfllow(0);
		            courseBehave.setAnswer("");
		            courseBehave.setClasswork("");
		            courseBehave.setScid(singleCourse.getScid());
		      //      System.out.println(singleCourse.getScid());
		            courseBehave.setTeacher_id(tid);
		            factory.getCurrentSession().merge(courseBehave);
				}
				else
				{
					CourseBehave courseBehave = (CourseBehave) factory.getCurrentSession()
							.createQuery("from CourseBehave where scid = ? and teacher_id = ? and sno = ?")
							.setParameter(0, singleCourse.getScid())
							.setParameter(1, tid)
							.setParameter(2, student.getSno())
							.list().get(0);
					factory.getCurrentSession().merge(courseBehave);
				}
	        }
			studentlist.clear();
		}
		return 1;
	}

}
